using System.Collections.Generic;
using Godot;

public partial class NodePool : Node2D
{
	private readonly Queue<Node2D> pool = new();

	private static void SetActive(Node2D node, bool active)
	{
		node.Visible = active;
		node.ProcessMode = active ? ProcessModeEnum.Inherit : ProcessModeEnum.Disabled;
	}

	public Node2D GetNode(PackedScene scene)
	{
		Node2D node;
		if (pool.Count <= 0)
		{
			node = scene.Instantiate<Node2D>();
		}
		else
		{
			node = pool.Dequeue();
		}
		SetActive(node, true);
		return node;
	}

	public void PutNode(Node2D node)
	{
		SetActive(node, false);
		pool.Enqueue(node);
	}

	public void Clear()
	{
		foreach (var node in pool)
		{
			node.QueueFree();
		}
	}
}